/*
 * StartApplication.java
 *
 * Created on Sep 13, 2008 4:32:36 PM;
 */

package org.sunspotworld;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.util.*;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {


         
    final long ASSURED_BANDWIDTH=10;
    final long MAXIMUM_BANDWIDTH=100000;
    final double ABOVE_BANDWIDTH_PACKET_LOSS_RATIO=1.2;

    final long DURATION_OF_EACH_ITERATION=5*1000;
    // the duration of transfer

    final String DISCOVERY_PORT="10";
    final String TRANSMIT_PORT="11";
    

    private EDemoBoard eDemoBoard=EDemoBoard.getInstance();
    private AirText airText=new AirText(eDemoBoard);
    {
        airText.setColor(0, 0, 100);
    }
    private ModeOfOperation myMode;
    private long otherSpotAddress;
    protected void startApp() throws MIDletStateChangeException {
        new BootloaderListener().start();   // monitor the USB (if connected) and recognize commands from host

        Thread.currentThread().setPriority(Thread.MIN_PRIORITY+1);

        // Give the user to choose his spot's participation - active or passive
        ChooseModeOfOperation chooseModeOfOperation=new ChooseModeOfOperation(eDemoBoard, airText);
        myMode=chooseModeOfOperation.chooseMasterOrSlave();
        
        DiscoverOtherSpot discoverOtherSpot=new DiscoverOtherSpot(myMode, TRANSMIT_PORT);
        otherSpotAddress=discoverOtherSpot.findOtherSpotAddress();
        System.out.println("Other SPOT address is " + Long.toString(otherSpotAddress));
        
        if (myMode.equals(ModeOfOperation.PASSIVE)) {
           System.out.println("Finding the bandwidth - passive");
           // The passive side will wait for information on transfer
           // and will return the transfer information back to active side
           PassiveBandwidthBenchmark passiveBandwidthBenchmark=new PassiveBandwidthBenchmark(otherSpotAddress, TRANSMIT_PORT);
           passiveBandwidthBenchmark.FindTheBandwidthBenchmarkPassive();
        } else {
           System.out.println("Finding the bandwidth - active");
           // The active will send data and will adjust its "window size" accordingly 
           ActiveBandwidthBenchmark activeBandwidthBenchmark=new ActiveBandwidthBenchmark(ASSURED_BANDWIDTH, MAXIMUM_BANDWIDTH, ABOVE_BANDWIDTH_PACKET_LOSS_RATIO, DURATION_OF_EACH_ITERATION, otherSpotAddress, TRANSMIT_PORT);
           activeBandwidthBenchmark.FindTheBandwidthBenchmarkActive();
        }
        MyLogger.logLn("Finished all my duties");
        
        notifyDestroyed();                      // cause the MIDlet to exit

    
    }


    

    
    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }

}
